package com.admxj.mybatis.demo.executor;

import com.admxj.mybatis.demo.MybatisDemo;
import org.apache.ibatis.executor.BatchExecutor;
import org.apache.ibatis.executor.CachingExecutor;
import org.apache.ibatis.executor.ReuseExecutor;
import org.apache.ibatis.executor.SimpleExecutor;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransaction;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;

import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

/**
 * @author admxj
 * @version Id: ExecutorDemo, v 0.1 2020/5/31 2:21 上午 admxj Exp $
 */
public class ExecutorDemo extends MybatisDemo {


  @Test
  public void testSimple() throws SQLException {
    // 简单预处理器
    SimpleExecutor executor = new SimpleExecutor(configuration, jdbcTransaction);
    MappedStatement mappedStatement = configuration.getMappedStatement("com.admxj.mybatis.demo.SwitchDAO.updateStatus");

    executor.doUpdate(mappedStatement, "10");
    executor.doUpdate(mappedStatement, "10");
    executor.doUpdate(mappedStatement, "10");

//    System.out.println(objects);
//
//    SqlSession sqlSession = sessionFactory.openSession();
//    sqlSession.selectList()
//    sqlSession.selectOne()


  }

  @Test
  public void testReuse() throws SQLException {
    ReuseExecutor executor = new ReuseExecutor(configuration, jdbcTransaction);
    MappedStatement mappedStatement = configuration.getMappedStatement("com.admxj.mybatis.demo.SwitchDAO.updateStatus");


    executor.doUpdate(mappedStatement, "10");
    executor.doUpdate(mappedStatement, "10");
    executor.doUpdate(mappedStatement, "10");

  }

  @Test
  public void testBatch() throws SQLException {
    BatchExecutor executor = new BatchExecutor(configuration, jdbcTransaction);
    MappedStatement mappedStatement = configuration.getMappedStatement("com.admxj.mybatis.demo.SwitchDAO.updateStatus");

    // 设置参数()
    executor.doUpdate(mappedStatement, "10");
    executor.doUpdate(mappedStatement, "10");
    executor.doUpdate(mappedStatement, "10");

    // 批量刷新
    executor.commit(true);

  }

  @Test
  public void testCache() throws SQLException {



    SimpleExecutor executor = new SimpleExecutor(configuration, jdbcTransaction);
    MappedStatement mappedStatement = configuration.getMappedStatement("com.admxj.mybatis.demo.SwitchDAO.selectByDep");
    executor.query(mappedStatement, "SHA", RowBounds.DEFAULT, SimpleExecutor.NO_RESULT_HANDLER);

    executor.query(mappedStatement, "SHA", RowBounds.DEFAULT, SimpleExecutor.NO_RESULT_HANDLER);

  }

  @Test
  public void testCaching() throws SQLException {
    SimpleExecutor executor = new SimpleExecutor(configuration, jdbcTransaction);
    CachingExecutor cachingExecutor = new CachingExecutor(executor);
    MappedStatement mappedStatement = configuration.getMappedStatement("com.admxj.mybatis.demo.SwitchDAO.selectByDep");
    cachingExecutor.query(mappedStatement, "SHA", RowBounds.DEFAULT, SimpleExecutor.NO_RESULT_HANDLER);

//    cachingExecutor.commit(true);

    cachingExecutor.query(mappedStatement, "SHA", RowBounds.DEFAULT, SimpleExecutor.NO_RESULT_HANDLER);

  }


}
